package com.lbh.platforms.windows;

import com.lbh.structures.common.Transform;
import com.lbh.structures.math.Quaternion;
import com.lbh.structures.math.Vector3;
import com.sun.jna.Structure;

/***
 * project:JRuntime
 * package:com.lbh.structures
 *
 * @author user:lenovo QQ:1601078366
 * @date 2021/10/18 0:03
 * @version V1.0
 */
public class WinGameObject {

    public static class ByReference extends WinGameObject implements Structure.ByReference { }
    public static class ByValue extends WinGameObject implements Structure.ByValue { }

    public Transform transform;

    public WinGameObject[] children;
    private int count;

    public WinMesh mesh;

    public static WinGameObject createPrimitive(int type){
        WinGameObject go = new WinGameObject();
        go.transform = new Transform();
        go.transform.position = Vector3.zero.clone();
        go.transform.rotation = Quaternion.none.clone();
        go.transform.scale = new Vector3(1.0F, 1.0F, 1.0F);
        go.children = new WinGameObject[0];
        go.count = 0;
        go.mesh = WinMesh.createPrimitive(type);
        return go;
    }

    void rotateQuaternion(Quaternion q){
        float[] mat = new float[16];
        float x2 = q.x * q.x, y2 = q.y * q.y, z2 = q.z * q.z;
        float xy = q.x * q.y, yz = q.y * q.z, zw = q.z * q.w;
        float xz = q.x * q.z, yw = q.y * q.w, xw = q.x * q.w;
        mat[0] = 1.0f - 2.0f * (y2 + z2);
        mat[1] = 2.0f * (xy - zw);
        mat[2] = 2.0f * (xz - yw);
        mat[3] = 0.0f;
        mat[4] = 2.0f * (xy + zw);
        mat[5] = 1.0f - 2.0f * (z2 + x2);
        mat[6] = 2.0f * (yz - xw);
        mat[7] = 0.0f;
        mat[8] = 2.0f * (xz - yw);
        mat[9] = 2.0f * (yz + xw);
        mat[10] = 1.0f - 2.0f * (x2 + y2);
        mat[11] = 0.0f;
        mat[12] = 0.0f;
        mat[13] = 0.0f;
        mat[14] = 0.0f;
        mat[15] = 1.0f;
        WinOpenGL32.ins.glMultMatrixf(mat);
    }

    public void render(){
        WinOpenGL32.ins.glPushMatrix();
        WinOpenGL32.ins.glTranslatef(transform.position.x, transform.position.y, transform.position.z);
        WinOpenGL32.ins.glRotatef(2.0F * 57.29577951308F * (float)Math.acos(transform.rotation.w),
                transform.rotation.x, transform.rotation.y, transform.rotation.z);
        WinOpenGL32.ins.glScalef(transform.scale.x, transform.scale.y, transform.scale.z);
        if (mesh != null) {
            mesh.draw();
        }
        for (WinGameObject child : children) {
            if (child != null){
                child.render();
            }
        }
        WinOpenGL32.ins.glPopMatrix();
    }

    public <T> T getComponent(){
        return null;
    }

    public <T> T[] getComponents(){
        return null;
    }

    public <T> T addComponent(){
        return null;
    }

    public <T> T[] addComponents(){
        return null;
    }

    public <T> T removeComponent(){
        return null;
    }

    public <T> T[] removeComponents(){
        return null;
    }
}
